//
//  MTDragRefreshView.m
//  MobTimes
//
//  Created by 江山 王 on 5/4/12.
//  Copyright (c) 2012 Finalist. All rights reserved.
//

#define MTDragRefreshViewWidth                      (320.0f)

#define MTDraggingInfoLabelWidth                    (MTDragRefreshViewWidth/4.0f)
#define MTDraggingInfoLabelHeight                   (20.0f)

#define MTArrowImgViewWidth                         (30.0f)
#define MTArrowImgViewHeight                        (30.0f)

#define MTLastDraggingTimeLabelWidth                (MTDragRefreshViewWidth/2.0f)
#define MTLastDraggingTimeLabelHeight               (20.0f)

#import "MTDragRefreshView.h"
#import <QuartzCore/QuartzCore.h>

@implementation MTDragRefreshView

@synthesize dragginginfoLabel = _dragginginfoLabel;
@synthesize arrowImgView = _arrowImgView;
@synthesize lastDraggingTimeLabel = _lastDraggingTimeLabel;

#pragma mark - Lifecycle

- (id)init {
    CGRect _selfFrame = CGRectMake(0,-MTDragRefreshViewHeight, MTDragRefreshViewWidth, MTDragRefreshViewHeight);
    self = [super initWithFrame:_selfFrame];
    if (self) {
        _dragginginfoLabel = [[UILabel alloc] initWithFrame:CGRectMake((MTDragRefreshViewWidth - MTDraggingInfoLabelWidth)/2.0f,
                                                                       8.0f, 
                                                                       MTDraggingInfoLabelWidth, 
                                                                       MTDraggingInfoLabelHeight)];
        _dragginginfoLabel.font = [UIFont systemFontOfSize:15.0f];
        _dragginginfoLabel.textAlignment = UITextAlignmentCenter;
        _dragginginfoLabel.backgroundColor = [UIColor clearColor];
        [_dragginginfoLabel setText:MT_String("下拉刷新")];
        [self addSubview:_dragginginfoLabel];
        
        _arrowImgView = [[UIImageView alloc] initWithFrame:CGRectMake((MTDragRefreshViewWidth)/6.0f,
                                                                      10.0f, 
                                                                      MTArrowImgViewWidth, 
                                                                      MTArrowImgViewHeight)];
        _arrowImgView.image = [UIImage imageNamed:MT_String("dragging_fresh_arrow.png")];
        [self addSubview:_arrowImgView];
        
        _lastDraggingTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake((MTDragRefreshViewWidth - MTLastDraggingTimeLabelWidth)/2.0f,
                                                                           _dragginginfoLabel.frame.origin.y+_dragginginfoLabel.frame.size.height+3, 
                                                                           MTLastDraggingTimeLabelWidth, 
                                                                           MTLastDraggingTimeLabelHeight)];
        _lastDraggingTimeLabel.font = [UIFont systemFontOfSize:12.0f];
        _lastDraggingTimeLabel.textAlignment = UITextAlignmentCenter;
        _lastDraggingTimeLabel.backgroundColor = [UIColor clearColor];
        [_lastDraggingTimeLabel setText:MT_String("上次刷新时间：刚刚")];
        [self addSubview:_lastDraggingTimeLabel];
        
        _activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        _activityView.frame = _arrowImgView.frame;
        _activityView.hidesWhenStopped  = YES;
        [self addSubview:_activityView];
        
        self.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        
    }
    return self;
}

- (void)dealloc {
    self.dragginginfoLabel = nil;
    self.arrowImgView = nil;
    self.lastDraggingTimeLabel = nil;
    [_activityView release];
    _activityView = nil;
    
    [super dealloc];
}

#pragma mark - Public methods implementation

- (void)setStatus:(MTTableHeaderDragRefreshStatus)status {
	switch (status) {
		case MTTableHeaderDragRefreshReleaseToReload: { 
			[self showActivity:NO animated:NO];
			[self setImageFlipped:YES];
			_dragginginfoLabel.text = MT_String("释放后刷新");
			break;
		}
		case MTTableHeaderDragRefreshPullToReload: {
			[self showActivity:NO animated:NO];
			[self setImageFlipped:NO];
			_dragginginfoLabel.text = MT_String("下拉刷新");
			break;
		}
		case MTTableHeaderDragRefreshLoading: {
			[self showActivity:YES animated:YES];
			[self setImageFlipped:NO];
			_dragginginfoLabel.text = MT_String("正在刷新...");
			break;
		}
		default: {
			break;
		}
	}
}

#pragma mark - Private methods implementation

- (void)showActivity:(BOOL)shouldShow animated:(BOOL)animated {
    if (shouldShow) {
        [_activityView startAnimating];
    } else {
        [_activityView stopAnimating];
    }
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:(animated ? kDefaultFastTransitionDuration : 0.0)];
    _arrowImgView.alpha = (shouldShow ? 0.0 : 1.0);
    [UIView commitAnimations];
}

- (void)setImageFlipped:(BOOL)flipped {
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:kDefaultFastTransitionDuration];
    [_arrowImgView layer].transform = (flipped ?
                                     CATransform3DMakeRotation(M_PI * 2, 0.0f, 0.0f, 1.0f) :
                                     CATransform3DMakeRotation(M_PI, 0.0f, 0.0f, 1.0f));
    [UIView commitAnimations];
}

@end
